package org.nbict.iot.trident.mogo;

import com.mongodb.client.model.Filters;
import org.apache.storm.mongodb.common.QueryFilterCreator;
import org.apache.storm.tuple.ITuple;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songseven on 18/6/26.
 */
public class MultiQueryFilterCreator implements QueryFilterCreator {

    private List<QueryField> fields;

    public MultiQueryFilterCreator() {
        this.fields = new ArrayList<QueryField>();
    }

    @Override
    public Bson createFilter(ITuple tuple) {
        List filters = new ArrayList<Bson>();
        fields.forEach(f -> {
            filters.add(f.op().filter(f.name(), tuple.getValueByField(f.name
                    ())));
        });
        return Filters.and(filters);
    }

    public MultiQueryFilterCreator eq(String name) {
        fields.add(new QueryField(name, new Filterable() {
            @Override
            public Bson filter(String name, Object val) {
                return Filters.eq(name, val);
            }
        }));

        return this;
    }

    public MultiQueryFilterCreator gt(String name) {
        fields.add(new QueryField(name, new Filterable() {
            @Override
            public Bson filter(String name, Object val) {
                return Filters.gt(name, val);
            }
        }));
        return this;
    }

    public MultiQueryFilterCreator lt(String name) {
        fields.add(new QueryField(name, new Filterable() {
            @Override
            public Bson filter(String name, Object val) {
                return Filters.lt(name, val);
            }
        }));

        return this;
    }

    public static void main(String[] args) {
        List filters = new ArrayList<Bson>();
        filters.add(Filters.eq("aaaa", 1000));
        filters.add(Filters.lt("bbbb", 200));

        //System.out.println(Filters.and(filters));
    }
}
